/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package es.us.mwm.service;

import es.us.mwm.ejb.AlertsEJBSingle;
import es.us.mwm.entities.Alerts;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Stateless;
import javax.ejb.embeddable.EJBContainer;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;


/**
 *
 * @author y0rg
 */
@Stateless
@Path("alerts") //http://localhost:8080/Alertas112/webresources/alerts
public class AlertsFacadeREST extends AbstractFacade<Alerts> {
    
    @PersistenceContext(unitName = "Alertas112PU")
    private EntityManager em;
          
    public AlertsFacadeREST() {
        super(Alerts.class);
           
    }

    /**
     * Metodo para añadir una alerta desde el webservice
     *
     * @param type
     * @param longitude
     * @param latitude
     * @param name
     * @param phone
     * @param description
     */
    @POST
    @Produces("application/xml")
    public void getXML(@FormParam("type") String type, @FormParam("longitude") String longitude, @FormParam("latitude") String latitude,
            @FormParam("name") String name, @FormParam("phone") String phone, @FormParam("description") String description) {
         
        //Alerts alert = new Alerts(type, Double.parseDouble(latitude), Double.parseDouble(longitude), description, Long.parseLong(phone), name);
        
        /* ESTO VA
        Alerts alert = new Alerts();
        alert.setDate(new Date());
        alert.setDescription("");
        alert.setLatitude(Double.parseDouble(latitude));
        alert.setLongitude(Double.parseDouble(longitude));
        alert.setName(name);
        alert.setPhone(Integer.parseInt(phone));
        alert.setType(type);
        alert.setUser(null);
        //System.out.println("ESTOY AQUI->" + alert.toString());        
        super.create(alert);
     */
       
       
    }
    
    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Alerts entity) {
        super.create(entity);
    }

    @PUT
    @Path("{id}")
    @Consumes({"application/xml", "application/json"})
    public void edit(@PathParam("id") Long id, Alerts entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Long id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Alerts find(@PathParam("id") Long id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Alerts> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Alerts> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
